package cn.com.huzo.service.trade.impl;

import cn.com.huzo.model.entities.trade.ProductionInstructionLinesSuitStyleNoEntity;
import cn.com.huzo.repository.trade.ProductionInstructionLinesRepository;
import cn.com.huzo.repository.trade.ProductionInstructionLinesSuitStyleNoRepository;
import cn.com.huzo.service.trade.ProductionInstructionLinesSuitStyleNoManager;
import huzofw.service.impl.GenericManagerImpl;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 生产指示书款号管理服务对象类实现。
 *
 * @author 吴旭
 */
@Service("ProductionInstructionLinesSuitStyleNoManager")
public class ProductionInstructionLinesSuitStyleNoManagerImpl extends GenericManagerImpl<ProductionInstructionLinesSuitStyleNoEntity, String>
        implements ProductionInstructionLinesSuitStyleNoManager {

    @Resource(name = "ProductionInstructionLinesRepository")
    private ProductionInstructionLinesRepository _dao_lines;

    @Resource(name = "ProductionInstructionLinesSuitStyleNoRepository")
    private ProductionInstructionLinesSuitStyleNoRepository _dao_lines_suit_styleno;

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public List<ProductionInstructionLinesSuitStyleNoEntity> fetchByHeaderId(Long headerId) throws Exception {
        return _dao_lines_suit_styleno.fetchByHeaderId(headerId);
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Map<String, Object> saveLines(Map<String, ? extends List<ProductionInstructionLinesSuitStyleNoEntity>> criterion) throws Exception {
        Map<String, Object> _result = new HashMap();
        int _rowsAffected1 = 0;
        int _rowsAffected2 = 0;
        long _headerId = 0l;

        if (criterion.get("insertList").size() != 0) {
            _headerId = criterion.get("insertList").get(0).getHeaderId();
            _rowsAffected1 = _dao_lines_suit_styleno.batchInsert(criterion.get("insertList"));
            // 还要在款尺码表增加相应的数据
        }
        if (criterion.get("updateList").size() != 0) {
            _headerId = criterion.get("updateList").get(0).getHeaderId();
            _rowsAffected2 = _dao_lines_suit_styleno.batchUpdate(criterion.get("updateList"));
        }

        if ((_rowsAffected1 + _rowsAffected2) > 0) {
            _result.put("Success", true);
            _result.put("StyleNo", StringUtils.join(_dao_lines_suit_styleno.fetchByHeaderId(_headerId).toArray(), "/"));
            _result.put("LinesList", _dao_lines.fetchLinesByHeaderIdAndStyleNo(_headerId, null));
        } else {
            _result.put("Success", false);
        }

        return _result;
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Override
    @Transactional
    public Map<String, Object> delete(Map<String, Object> criterion) throws Exception {
        Map<String, Object> _result = new HashMap();

        Integer rowsAffected = _dao_lines_suit_styleno.delete(MapUtils.getString(criterion, "LinesId"));
        if (rowsAffected > 0) {
            _result.put("Success", true);
            _result.put("StyleNo", StringUtils.join(_dao_lines_suit_styleno.fetchByHeaderId(MapUtils.getLong(criterion, "HeaderId")).toArray(), "/"));
            _result.put("LinesList", _dao_lines.fetchLinesByHeaderIdAndStyleNo(MapUtils.getLong(criterion, "HeaderId"), null));
        } else {
            _result.put("Success", false);
        }

        return _result;
    }
}
